﻿define(function (require) {
    var utility = require('ezdesk/utility/utility.js');

    return Backbone.View.extend({
        //... is a list tag.
        tagName: "li",
        template: _.template($("#li_update").html()),
        events: {
            "click .feed-close": "clear"
            , "click .ui-link-inherit": "setIsRead"
        },

        initialize: function () {
            _.bindAll(this, 'render', 'setIsRead');
            this.model.bind('change', this.render);
            this.model.view = this;

            var $self = this;
            $(this.el).attr('data-theme', 'c')
                      .addClass('ui-btn ui-btn-icon-right ui-li ui-btn-up-c');

            if (this.model.get('IsRead'))
                $(this.el).find('.title').addClass('is-read');

        },

        render: function () {
            var _model = this.model;
            $(this.el).html(this.template(_model.toJSON()));

            var element = $(this.el)[0];

            $(element).find('.feed-close').data('cid', this.model.cid);
            var alink = $(element).find('.body').find('.ezlink');

            alink.each(function () {
                //去掉连接中的回车 和 一些 带有空内容的连接
                if (this.firstChild.innerHTML == "<br>" || this.firstChild.innerHTML == "") return;

                if ($(this).attr("href").indexOf('target=action') != -1) {
                    $(this).addClass('actionLink');
                }

                $(this).appendTo($(element).find(".action-r"));
            })

            if (_model.get('IsRead'))
                $(element).find('.title').addClass('is-read');

            $(this.el).find('a.ezlink').each(function (index, item) {
                utility.HandleATag(item, function () {
                    _model.setIsRead(true);
                });

            });

            this.model.bind('setFeedRead', function (unreadFeedCount) {
                $.postMessage('unreadFeeds:' + unreadFeedCount, '*', parent);
            });

            return this;
        },

        // Remove the item, destroy the model.
        clear: function (e) {
            //edit feed status
            this.model.setIsAvailable();
        },
        setIsRead: function () {
            //如果是未读 设为已读
            if (!this.model.get("IsRead"))
                this.model.setIsRead(true);
        }
    });
});